const path = require('path');
const send = require('koa-send');
const hwConfig = require('../consts/hwapi');
const { dbs } = require('../../libs/index');
const router = require('koa-router')()

router.get('/', async (ctx, next) => {
  if (checkPhone(ctx.request) || ctx.query.m == 'mobile') {
    await send(ctx, 'wwwroot/mobile.html', { root: process.env.ROOT });
  } else {
    await send(ctx, 'wwwroot/portal.html', { root: process.env.ROOT });
  }
});

router.get('/home', async (ctx, next) => {
  await ctx.render('index', {
    title: '工作台',
    dbs: dbs,
    servers: hwConfig.getAllServers()
  });
});

router.get('/docs', async (ctx, next) => {
  let nomenu = !!ctx.request.query.nomenu;

  await ctx.render('docs', {
    title: 'api接口文档',
    nomenu: nomenu
  });
});

function checkPhone (request) {
  let userAgent = (request.header['user-agent'] || '').toLowerCase();
  let patPhone = /ipad|iphone os|midp|rv:1.2.2.3|ucweb|android|windows ce| windows mobile/;

  return patPhone.test(userAgent);
}

module.exports = router;